package cz.muni.fi.pa165.musiclibrary.impl.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.Validate;
import org.springframework.transaction.annotation.Transactional;

import cz.muni.fi.pa165.musiclibrary.impl.database.dao.IArtistDAO;
import cz.muni.fi.pa165.musiclibrary.impl.database.entity.IArtist;
import cz.muni.fi.pa165.musiclibrary.api.service.IArtistService;
import cz.muni.fi.pa165.musiclibrary.api.service.dto.IArtistDTO;
import cz.muni.fi.pa165.musiclibrary.impl.service.dto.translator.ArtistDTOTranslator;

/**
*
* @author Martin Szabo
* 2012-11-02T23:04:07+0100
*/
@Transactional
public final class ArtistService implements IArtistService{

    private IArtistDAO artistDAO;
    
    public ArtistService(final IArtistDAO artistDAO) {
        this.artistDAO = artistDAO;
    }
    
    @Override
    public void create(IArtistDTO artist, String userName) {
        Validate.isTrue(artist != null, "Artist should not be null");
        artistDAO.create(ArtistDTOTranslator.translateFromDTO(artist));
    }

    @Override
    public void update(IArtistDTO artist, String userName) {
        Validate.isTrue(artist != null, "Artist should not be null");
        artistDAO.update(ArtistDTOTranslator.translateFromDTO(artist));
    }

    @Override
    public void delete(IArtistDTO artist, String userName) {
        Validate.isTrue(artist != null, "Artist should not be null");
        artistDAO.delete(ArtistDTOTranslator.translateFromDTO(artist));
    }

    @Override
    public IArtistDTO getById(final Long idArtist) {
        Validate.isTrue(idArtist != null, "Artist id should not be null");
        return ArtistDTOTranslator.translateToDTO(artistDAO.getById(idArtist));
    }
    
    @Override
    public List<IArtistDTO> getByName(final String name) {
        Validate.isTrue(name != null, "Artist name should not be null");
        List<IArtistDTO> ArtistsDTO = new ArrayList<IArtistDTO>();
        List<IArtist> allArtists = artistDAO.getByName(name);
        for(IArtist artist : allArtists) {
            ArtistsDTO.add(ArtistDTOTranslator.translateToDTO(artist));
        }
        return ArtistsDTO;
    }

    @Override
    public List<IArtistDTO> getAll() {
        List<IArtistDTO> allArtistsDTO = new ArrayList<IArtistDTO>();
        List<IArtist> allArtists = artistDAO.getAll();
        for(IArtist artist : allArtists) {
            allArtistsDTO.add(ArtistDTOTranslator.translateToDTO(artist));
        }
        return allArtistsDTO;
    }

}